home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / man / lib.fmt / c / Opt.man < prev    next >
Encoding:
Text File  |  1991-02-08  |  14.6 KB  |  399 lines

  1.  
  2.  
  3.  
  4. Opt                   C Library Procedures                    Opt
  5.  
  6.  
  7.  
  8. _________________________________________________________________
  9.  
  10. NNAAMMEE
  11.      Opt_Parse, Opt_PrintUsage - Manage command line options
  12.  
  13. SSYYNNOOPPSSIISS
  14.      ##iinncclluuddee <<ooppttiioonn..hh>>
  15.  
  16.      int OOpptt__PPaarrssee(_a_r_g_c, _a_r_g_v, _o_p_t_i_o_n_A_r_r_a_y, _n_u_m_O_p_t_i_o_n_s, _f_l_a_g_s)
  17.  
  18.      void OOpptt__PPrriinnttUUssaaggee(_c_o_m_m_a_n_d_N_a_m_e, _o_p_t_i_o_n_A_r_r_a_y, _n_u_m_O_p_t_i_o_n_s)
  19.  
  20. AARRGGUUMMEENNTTSS
  21.      int      _a_r_g_c           (in)      Number  of  arguments   on
  22.                                        command line.
  23.  
  24.      char     **_a_r_g_v         (in/out)  Command   line   arguments
  25.                                        passed to main program.
  26.  
  27.      char     *_c_o_m_m_a_n_d_N_a_m_e   (in)      Name of the program  being
  28.                                        run, usually _a_r_g_v[0].
  29.  
  30.      Option   *_o_p_t_i_o_n_A_r_r_a_y   (in)      An   array    of    option
  31.                                        descriptions.
  32.  
  33.      int      _n_u_m_O_p_t_i_o_n_s     (in)      Number  of   elements   in
  34.                                        optionArray,       usually
  35.                                        obtained     using     the
  36.                                        OOpptt__NNuummbbeerr   macro,   e.g.
  37.                                        OOpptt__NNuummbbeerr(optionArray).
  38.  
  39.      int      _f_l_a_g_s          (in)      If   non-zero,   then   it
  40.                                        specifies   one   or  more
  41.                                        flags  that  control   the
  42.                                        parsing    of   arguments.
  43.                                        Different  flags  may   be
  44.                                        OR'ed  together.  The only
  45.                                        flags  currently   defined
  46.                                        are   OPT_ALLOW_CLUSTERING
  47.                                        and OPT_OPTIONS_FIRST.
  48.  
  49. _________________________________________________________________
  50.  
  51. DDEESSCCRRIIPPTTIIOONN
  52.      OOpptt__PPaarrssee parses the command line  arguments  of  a  program
  53.      according to an array of option descriptions.  Starting with
  54.      _a_r_g_v[1], it parses as many options as it can and returns the
  55.      rest  of  the  options  in  the _a_r_g_v array, compacted to the
  56.      beginning of the array (starting with _a_r_g_v[1]).  The  return
  57.      value  indicates  how  many options are returned in the _a_r_g_v
  58.      array (including _a_r_g_v[0]).  Opt_Parse returns  options  that
  59.      don't start with ``-'' unless they are arguments for options
  60.      that it parses.  OOpptt__PPaarrssee also returns any options  follow-
  61.      ing an OOPPTT__RREESSTT option (see below for more details).
  62.  
  63.  
  64.  
  65. Sprite v.1.0       Printed:  February 8, 1991                   1
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72. Opt                   C Library Procedures                    Opt
  73.  
  74.  
  75.  
  76.      Each element of the  array  _o_p_t_i_o_n_A_r_r_a_y  has  the  following
  77.      structure:
  78.  
  79.           ttyyppeeddeeff ssttrruucctt Option {
  80.               iinntt _t_y_p_e;
  81.               cchhaarr*_k_e_y;
  82.               cchhaarr*_v_a_l_u_e_P_t_r;
  83.               cchhaarr*_d_o_c_M_s_g;
  84.           } Option;
  85.  
  86.      The _k_e_y field is a string that identifies this option.   For
  87.      example,  if  _k_e_y is ffoooo, then this Option will match a --ffoooo
  88.      command-line option.  If _k_e_y is the empty string (``'') then
  89.      it  matches  a  --  command-line option.  If _k_e_y is NULL, the
  90.      Option will not match any command-line options (this feature
  91.      is only useful for OPT_DOC options).  _D_o_c_M_s_g is a documenta-
  92.      tion string to print out as part of  a  help  message.   The
  93.      _t_y_p_e  field  determines  what  to  do  when  this  Option is
  94.      matched.  It also determines the  meaning  of  the  _v_a_l_u_e_P_t_r
  95.      field.   _T_y_p_e  should  always  be specified using one of the
  96.      following macros:
  97.  
  98.      OOPPTT__TTRRUUEE
  99.           Treats _v_a_l_u_e_P_t_r as  the  address  of  an  integer,  and
  100.           stores the value 1 in that integer.
  101.  
  102.      OOPPTT__FFAALLSSEE
  103.           Treats _v_a_l_u_e_P_t_r as the address of an integer and stores
  104.           the value 0 in that integer.
  105.  
  106.      OOPPTT__CCOONNSSTTAANNTT(_v_a_l_u_e)
  107.           This is a generalized form of OPT_TRUE  and  OPT_FALSE.
  108.           Treats _v_a_l_u_e_P_t_r as the address of an integer and stores
  109.           _v_a_l_u_e in  that  integer.   _V_a_l_u_e  must  be  a  positive
  110.           integer.
  111.  
  112.      OOPPTT__IINNTT
  113.           The next argument after the one matching _k_e_y must  con-
  114.           tain an integer string in the format accepted by ssttrrttooll
  115.           (e.g. ``0'' and ``0x'' prefixes may be used to  specify
  116.           octal  or hexadecimal numbers, respectively).  _V_a_l_u_e_P_t_r
  117.           will be treated as the address of an integer,  and  the
  118.           value given by the next argument will be stored there.
  119.  
  120.      OOPPTT__TTIIMMEE
  121.           The next argument after the one matching _k_e_y must  con-
  122.           tain  a  string  that  is  parsable as a date and time.
  123.           Currently, only two formats are recognized:
  124.  
  125.           _s_e_c_o_n_d_s
  126.  
  127.           and
  128.  
  129.  
  130.  
  131. Sprite v.1.0       Printed:  February 8, 1991                   2
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. Opt                   C Library Procedures                    Opt
  139.  
  140.  
  141.  
  142.           _y_y.._m_m.._d_d.._h_h.._m_m.._s_s
  143.  
  144.           The first form is simply the number  of  seconds  since
  145.           the  start  of the epoch (1 January 1970, 0 hours GMT).
  146.           The second form specifies the year (e.g., 91 or  1991),
  147.           month  (1-12),  day of the month, hours (0-23), minutes
  148.           (0-59), and seconds (0-59).  All fields must be  speci-
  149.           fied.   _V_a_l_u_e_P_t_r  will  be  treated as the address of a
  150.           ttiimmee__tt (defined in <<ttiimmee..hh>>), and the given  time  will
  151.           be stored there.  All times are in terms of the current
  152.           timezone and daylight savings rules.
  153.  
  154.           Note that this flavor can  clobber  the  static  buffer
  155.           used by the llooccaallttiimmee library routine.
  156.  
  157.      OOPPTT__FFLLOOAATT
  158.           The next argument after the one matching _k_e_y must  con-
  159.           tain  a floating-point number in the format accepted by
  160.           ssttrrttooll.  _V_a_l_u_e_P_t_r will be treated as the address of  an
  161.           double-precision  floating  point  value, and the value
  162.           given by the next argument will be stored there.
  163.  
  164.      OOPPTT__SSTTRRIINNGG
  165.           Treats _v_a_l_u_e_P_t_r as the  address  of  a  (char  *),  and
  166.           stores  a  pointer to the next argument in the location
  167.           pointed to by _v_a_l_u_e_P_t_r.
  168.  
  169.      OOPPTT__DDOOCC
  170.           This option is intended primarily as a way of  printing
  171.           extra  documentation during help message printouts.  It
  172.           isn't normally used as an actual option  (and  normally
  173.           its _k_e_y field is NULL).  If it is invoked as an option,
  174.           then the same thing happens as for the  ``-?''  option:
  175.           descriptions get printed for all options in _o_p_t_i_o_n_A_r_r_a_y
  176.           and OOpptt__PPaarrssee calls exit(0) to terminate the process.
  177.  
  178.      OOPPTT__RREESSTT
  179.           This option is used by programs  that  allow  the  last
  180.           several  of their options to be the name and/or options
  181.           for some other  program.   If  an  OOPPTT__RREESSTT  option  is
  182.           found,  then  OOpptt__PPaarrssee  doesn't  process  any  of  the
  183.           remaining arguments;  it returns them all at the begin-
  184.           ning  of  _a_r_g_v.  In addition, OOpptt__PPaarrssee treats _v_a_l_u_e_P_t_r
  185.           as the address of an integer value, and stores in  that
  186.           value the index of the first of the OOPPTT__RREESSTT options in
  187.           the returned _a_r_g_v.  This allows the program to  distin-
  188.           guish  the  OOPPTT__RREESSTT  options  from  other  unprocessed
  189.           options that preceeded the OOPPTT__RREESSTT.
  190.  
  191.      OOPPTT__FFUUNNCC
  192.           When one of these options is encountered,  _v_a_l_u_e_P_t_r  is
  193.           treated  as  the  address  of  a function which is then
  194.  
  195.  
  196.  
  197. Sprite v.1.0       Printed:  February 8, 1991                   3
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204. Opt                   C Library Procedures                    Opt
  205.  
  206.  
  207.  
  208.           called with the following calling sequence:
  209.  
  210.           iinntt
  211.           _f_u_n_c(_o_p_t_S_t_r_i_n_g, _n_e_x_t_A_r_g)
  212.               cchhaarr    *_o_p_t_S_t_r_i_n_g;
  213.               cchhaarr    *_n_e_x_t_A_r_g;
  214.           {
  215.           }
  216.  
  217.           The _o_p_t_S_t_r_i_n_g parameter points to the  current  option,
  218.           and  _n_e_x_t_A_r_g  points  to  the next option from _a_r_g_v (or
  219.           NULL if there aren't any more options in _a_r_g_v.  If _f_u_n_c
  220.           uses  _n_e_x_t_A_r_g  as an argument to the current option (so
  221.           that Opt_Parse should skip it), then it  should  return
  222.           1.  Otherwise it should return 0.
  223.  
  224.      OOPPTT__GGEENNFFUUNNCC
  225.           Treat _v_a_l_u_e_P_t_r as the address of a  function  and  pass
  226.           all  of  the remaining arguments to it in the following
  227.           way:
  228.  
  229.           iinntt
  230.           _g_e_n_f_u_n_c(_o_p_t_S_t_r_i_n_g, _a_r_g_c, _a_r_g_v)
  231.               cchhaarr    *_o_p_t_S_t_r_i_n_g;
  232.               iinntt _a_r_g_c;
  233.               cchhaarr    **_a_r_g_v;
  234.           {
  235.           }
  236.  
  237.           _A_r_g_c and _a_r_g_v refer to all of the options after the one
  238.           that  triggered  the  call, and _o_p_t_S_t_r_i_n_g points to the
  239.           triggering option.  _G_e_n_f_u_n_c should behave in a  fashion
  240.           similar  to  OOpptt__PPaarrssee:  parse as many of the remaining
  241.           arguments as it can, then return any that are  left  by
  242.           compacting  them  to the beginning of _a_r_g_v (starting at
  243.           _a_r_g_v[0]).  _G_e_n_f_u_n_c should return a count  of  how  many
  244.           arguments  are  left  in  _a_r_g_v;  OOpptt__PPaarrssee will process
  245.           them.
  246.  
  247.  
  248. FFLLAAGGSS
  249.      OOPPTT__AALLLLOOWW__CCLLUUSSTTEERRIINNGG
  250.           This  will  permit  several  options  to  be  clustered
  251.           together  with  a single ``-'', e.g., ``foo -abc'' will
  252.           be  handled  the  same  way  as  ``foo  -a   -b   -c''.
  253.           OPT_ALLOW_CLUSTERING   is  likely  to  cause  confusing
  254.           behavior unless each option is identified with a single
  255.           character.
  256.  
  257.      OOPPTT__OOPPTTIIOONNSS__FFIIRRSSTT
  258.           This causes OOpptt__PPaarrssee to stop parsing the command  line
  259.           anytime  something  that  is not an option is detected.
  260.  
  261.  
  262.  
  263. Sprite v.1.0       Printed:  February 8, 1991                   4
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270. Opt                   C Library Procedures                    Opt
  271.  
  272.  
  273.  
  274.           Thus, a program that takes some options followed  by  a
  275.           command  to execute (along with that command's options)
  276.           can parse  its  own  options  using  OPT_OPTIONS_FIRST.
  277.           When the command to execute is encountered, assuming it
  278.           does not begin with a hyphen, OOpptt__PPaarrssee will return the
  279.           command and its arguments starting at _a_r_g_v[1], ignoring
  280.           any arguments with hyphens  following  the  first  non-
  281.           option.
  282.  
  283. UUSSAAGGEE MMEESSSSAAGGEE
  284.      OOpptt__PPrriinnttUUssaaggee may be invoked to print out all the  documen-
  285.      tation  strings (plus option names and default values) for a
  286.      command's options.  If OOpptt__PPaarrssee encounters an option ``-?''
  287.      or  ``-help'',  then  it  will call OOpptt__PPrriinnttUUssaaggee and exit.
  288.      Note:  in some shells  the  question-mark  must  be  escaped
  289.      (e.g., ``foo -\?'' in _c_s_h).
  290.  
  291.  
  292. EEXXAAMMPPLLEE
  293.      Here is an example definition of a set  of  option  descrip-
  294.      tions  and  some  sample command lines that use the options.
  295.      Note the effect on argc and argv;   command  arguments  that
  296.      get  interpreted  as options or option values are eliminated
  297.      from argv, and argc is updated to reflect reduced number  of
  298.      arguments.
  299.  
  300.           /*
  301.            * Define and set default values for globals.
  302.            */
  303.           Boolean debugFlag = FALSE;
  304.           int numReps = 100;
  305.           char defaultFileName[] = "out";
  306.           char *fileName = defaultFileName;
  307.           Boolean exec = FALSE;
  308.  
  309.           /*
  310.            * Define option descriptions.
  311.            */
  312.           Option optionArray[] = {
  313.               OPT_TRUE, "X", (char *) &debugFlag, "Turn on debugging printfs",
  314.               OPT_INT, "N", (char *) &numReps, "Number of repetitions",
  315.               OPT_STRING, "of", (char *) &fileName, "Output filename",
  316.               OPT_REST, "x", (char *) &exec,
  317.                   "File to exec, followed by any arguments (must be last argument).",
  318.           };
  319.  
  320.           main(argc, argv)
  321.               int argc;
  322.               char *argv[];
  323.           {
  324.               Opt_Parse(argc, argv, optionArray, Opt_Number(optionArray),
  325.                       OPT_ALLOW_CLUSTERING);
  326.  
  327.  
  328.  
  329. Sprite v.1.0       Printed:  February 8, 1991                   5
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336. Opt                   C Library Procedures                    Opt
  337.  
  338.  
  339.  
  340.               /*
  341.                * the rest of the program.
  342.                */
  343.           }
  344.  
  345.      Note that default values can be  assigned  to  option  vari-
  346.      ables.   Also, numOptions gets calculated by the compiler in
  347.      this example.  Here are some example command lines and their
  348.      effects.
  349.  
  350.           prog -N 200 infile      # just sets the numReps variable to 200
  351.           prog -of out200 infile  # sets fileName to reference "out200"
  352.           prog -XN 10 infile      # sets the debug flag, also sets numReps
  353.      In  all  of  the  above  examples,  the  return  value  from
  354.      Opt_Parse  will be 2, _a_r_g_v[0] will be ``prog'', _a_r_g_v[1] will
  355.      be ``infile'', and _a_r_g_v[2] will be NULL.
  356.  
  357.  
  358. KKEEYYWWOORRDDSS
  359.      arguments, command line, options
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395. Sprite v.1.0       Printed:  February 8, 1991                   6
  396.  
  397.  
  398.  
  399.